using UnityEngine;
using QFramework;

namespace ProjectIndieFarm
{
    public partial class AudioController : ViewController, ISingleton
    {
        public bool IsWalkPlaying { get; private set; } = false;

        public int Walk1Frames;
        public int Walk2Frames;
        public int Walk3Frames;

        private IActionController mWalkAction;

        public static AudioController Instance
        {
            get
            {
                return MonoSingletonProperty<AudioController>.Instance;
            }
        }

        public void OnSingletonInit()
        {

        }

        public void PlayWalk()
        {
            if (IsWalkPlaying) return;

            if (mWalkAction == null)
            {
                mWalkAction = ActionKit.Repeat()
                    .Callback(() => SfxWalk1.Play())
                    .DelayFrame(Walk1Frames)
                    .Callback(() => SfxWalk2.Play())
                    .DelayFrame(Walk2Frames)
                    .Callback(() => SfxWalk3.Play())
                    .DelayFrame(Walk3Frames)
                    .Start(this);
            }

            IsWalkPlaying = true;
        }

        public void StopWalk()
        {
            if (!IsWalkPlaying) return;

            if (mWalkAction != null)
            {
                mWalkAction.Deinit();
                mWalkAction = null;
            }

            IsWalkPlaying = false;
        }
    }
}
